#!/usr/bin/env python3
"""
分析fused_score字段的范围问题
"""

def analyze_fused_score_ranges():
    """
    分析fused_score字段的范围问题
    """
    print("=" * 80)
    print("fused_score字段范围分析")
    print("=" * 80)
    
    # 从API响应中提取的实际数据
    jobs_data = [
        {"id": "44", "title": "Java开发工程师", "fused_score": 0.9722785486609351, "es_score": 646.4253, "chroma_score": 0.6152492761611938},
        {"id": "46", "title": "微服务开发工程师", "fused_score": 0.8419039857905875, "es_score": 525.24524, "chroma_score": 0.5935757160186768},
        {"id": "45", "title": "Java中级开发工程师", "fused_score": 0.7991654281194258, "es_score": 493.5186, "chroma_score": 0.5946286916732788},
        {"id": "127", "title": "Java分布式架构师", "fused_score": 0.05993526781603319, "es_score": 232.46751, "chroma_score": 0.0},
        {"id": "5", "title": "制造业成本会计", "fused_score": 0.009580185796427833, "es_score": 193.87051, "chroma_score": 0.0},
        {"id": "52", "title": "Java后端工程师", "fused_score": 5.168868772485151e-05, "es_score": 0.0, "chroma_score": 0.9062687158584595},
        {"id": "72", "title": "Java后端工程师", "fused_score": 5.168868772485151e-05, "es_score": 0.0, "chroma_score": 0.9062687158584595},
        {"id": "112", "title": "Java后端工程师", "fused_score": 5.168868772485151e-05, "es_score": 0.0, "chroma_score": 0.9062687158584595}
    ]
    
    print("原始fused_score数据:")
    for i, job in enumerate(jobs_data, 1):
        print(f"  {i}. {job['title']}: {job['fused_score']}")
    
    # 分析范围
    fused_scores = [job['fused_score'] for job in jobs_data]
    es_scores = [job['es_score'] for job in jobs_data]
    chroma_scores = [job['chroma_score'] for job in jobs_data]
    
    print(f"\n范围分析:")
    print(f"  fused_score范围: {min(fused_scores):.6f} - {max(fused_scores):.6f}")
    print(f"  ES分数范围: {min(es_scores):.2f} - {max(es_scores):.2f}")
    print(f"  ChromaDB分数范围: {min(chroma_scores):.6f} - {max(chroma_scores):.6f}")
    
    print(f"\n问题识别:")
    print(f"  • 前3个岗位: fused_score在0.8-1.0范围")
    print(f"  • 中间2个岗位: fused_score在0.01-0.06范围")
    print(f"  • 后3个岗位: fused_score在0.00005范围")
    print(f"  • 范围跨度: {max(fused_scores) / min(fused_scores):.0f}倍差异！")
    
    return jobs_data


def analyze_normalization_issues():
    """
    分析归一化问题
    """
    print("\n" + "=" * 80)
    print("归一化问题分析")
    print("=" * 80)
    
    # 模拟正确的归一化过程
    es_scores = [646.4253, 525.24524, 493.5186, 232.46751, 193.87051, 0.0, 0.0, 0.0]
    chroma_scores = [0.6152492761611938, 0.5935757160186768, 0.5946286916732788, 0.0, 0.0, 0.9062687158584595, 0.9062687158584595, 0.9062687158584595]
    
    print("正确的归一化过程:")
    
    # ES归一化
    es_min = min(es_scores)
    es_max = max(es_scores)
    es_range = es_max - es_min
    es_normalized = [(s - es_min) / es_range for s in es_scores]
    
    print(f"ES分数: {es_scores}")
    print(f"ES归一化: {[round(x, 3) for x in es_normalized]}")
    
    # ChromaDB已经是归一化的
    print(f"ChromaDB分数: {[round(x, 3) for x in chroma_scores]}")
    
    # 融合计算
    weight_es = 0.6
    weight_chroma = 0.4
    correct_fused = [weight_es * es_norm + weight_chroma * chroma for es_norm, chroma in zip(es_normalized, chroma_scores)]
    
    print(f"正确融合分数: {[round(x, 3) for x in correct_fused]}")
    
    # 实际分数
    actual_fused = [0.9722785486609351, 0.8419039857905875, 0.7991654281194258, 0.05993526781603319, 0.009580185796427833, 5.168868772485151e-05, 5.168868772485151e-05, 5.168868772485151e-05]
    
    print(f"实际融合分数: {[round(x, 3) for x in actual_fused]}")
    
    print(f"\n差异分析:")
    for i, (correct, actual) in enumerate(zip(correct_fused, actual_fused)):
        diff = abs(correct - actual)
        print(f"  岗位{i+1}: 正确={correct:.3f}, 实际={actual:.3f}, 差异={diff:.3f}")


def identify_problems():
    """
    识别具体问题
    """
    print("\n" + "=" * 80)
    print("问题识别")
    print("=" * 80)
    
    print("发现的问题:")
    print("1. 归一化范围不一致")
    print("   • 前3个岗位: 0.8-1.0 (正常范围)")
    print("   • 中间2个岗位: 0.01-0.06 (异常低)")
    print("   • 后3个岗位: 0.00005 (异常低)")
    
    print("\n2. 可能的原因:")
    print("   • ES分数归一化可能有问题")
    print("   • 权重设置可能不正确")
    print("   • 融合算法可能有bug")
    print("   • 数据预处理可能有问题")
    
    print("\n3. 影响:")
    print("   • 排序结果可能不准确")
    print("   • 分数无法直接比较")
    print("   • 用户体验受影响")
    
    print("\n4. 建议修复:")
    print("   • 检查归一化算法")
    print("   • 确保所有分数在[0,1]范围")
    print("   • 验证权重设置")
    print("   • 测试融合算法")


def suggest_fixes():
    """
    建议修复方案
    """
    print("\n" + "=" * 80)
    print("修复建议")
    print("=" * 80)
    
    print("1. 检查归一化函数:")
    print("   def normalize_scores(results, score_key):")
    print("       if not results:")
    print("           return {}")
    print("       scores = [r.get(score_key, 0.0) for r in results]")
    print("       min_score = min(scores)")
    print("       max_score = max(scores)")
    print("       if max_score - min_score < 1e-9:")
    print("           return {i: 1.0 for i in range(len(results))}")
    print("       return {i: (s - min_score) / (max_score - min_score) for i, s in enumerate(scores)}")
    
    print("\n2. 检查融合算法:")
    print("   for i, job in enumerate(es_results):")
    print("       es_norm = es_normalized.get(i, 0.0)")
    print("       chroma_norm = chroma_normalized.get(i, 0.0)")
    print("       fused_score = weight_es * es_norm + weight_chroma * chroma_norm")
    print("       job['fused_score'] = fused_score")
    
    print("\n3. 验证分数范围:")
    print("   • 所有fused_score应该在[0,1]范围")
    print("   • 分数应该连续分布")
    print("   • 不应该有异常小的值")


def main():
    """
    主函数
    """
    print("开始分析fused_score范围问题...")
    
    jobs_data = analyze_fused_score_ranges()
    analyze_normalization_issues()
    identify_problems()
    suggest_fixes()
    
    print("\n" + "=" * 80)
    print("分析完成！")
    print("=" * 80)


if __name__ == "__main__":
    main()
